package com.celesea.configuration.security.bean;

import org.springframework.security.core.userdetails.UserDetails;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class AuthorityUser implements UserDetails {
    private static final long serialVersionUID = -1041327031937199938L;

    /**
     * 最后更新时间
     */
    private Long updateTime;

    private Set<AuthorityInfo> authorities = new HashSet<AuthorityInfo>();

    /**
     * sso登录信息
     */
    private SsoLoginInfo ssoLoginInfo;

    /**
     * 登录名称
     */
    private String username;

    /**
     * 登录密码
     */
    private String password;

    private boolean isAccountNonExpired = true;

    private boolean isAccountNonLocked = true;

    private boolean isCredentialsNonExpired = true;

    private boolean isEnabled = true;


    public AuthorityUser() {
        super();
    }

    public AuthorityUser(SsoLoginInfo ssoLoginInfo, Long updateTime) {
        this();
        this.updateTime = updateTime;
        this.ssoLoginInfo = ssoLoginInfo;
        this.username = ssoLoginInfo.getUser().getName();
        this.password = ssoLoginInfo.getUser().getPassword();
        this.authorities = toAuthorities(ssoLoginInfo.getPermissionCollection().getPermissionCode());
    }

    private Set<AuthorityInfo> toAuthorities(Set<String> permissionCode) {
        return permissionCode.stream().map(s -> new AuthorityInfo(s)).collect(Collectors.toSet());
    }

    ;

    public SsoLoginInfo getSsoLoginInfo() {
        return ssoLoginInfo;
    }

    public void setSsoLoginInfo(SsoLoginInfo ssoLoginInfo) {
        this.ssoLoginInfo = ssoLoginInfo;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isAccountNonExpired() {
        return isAccountNonExpired;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        isAccountNonExpired = accountNonExpired;
    }

    public boolean isAccountNonLocked() {
        return isAccountNonLocked;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        isAccountNonLocked = accountNonLocked;
    }

    public boolean isCredentialsNonExpired() {
        return isCredentialsNonExpired;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        isCredentialsNonExpired = credentialsNonExpired;
    }

    public boolean isEnabled() {
        return isEnabled;
    }

    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
    }

    public Set<AuthorityInfo> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(Set<AuthorityInfo> authorities) {
        this.authorities = authorities;
    }

    public Long getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Long updateTime) {
        this.updateTime = updateTime;
    }
}
